﻿@model WebApp.Models.S_role
@{
    ViewBag.Title = "菜单权限";
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}
<div class="dialog-content">
    @Html.AntiForgeryToken()
    <input type="hidden" id="Id" value="@Model.Id" />
    <input type="hidden" id="Roleid" value="@Model.Roleid" />
    <input type="hidden" value="@(Model.Updatetime.HasValue ? Model.Updatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "")" class="form-control datafield" data-fieldname="Updatetime" />

    <div class="row">
        <div class="col-xs-12 nopadding">
            <div class="widget-content">
                <div class="row">
                    <ul id="treeview"></ul>
                </div>

            </div>
        </div>
    </div>
    <br />
    <br />
</div>
<div class="dialog-footer">
    <button id="btnclose" class="btn btn-small btn-default">关闭</button>
    <button id="btnsave" class="btn btn-small btn-primary">保存</button>
</div>
@section scripts{
    <script type="text/javascript">
        //回调函数
        var callback = "@ViewBag.CallBack";
        var menualljson=@Html.Raw(ViewBag.Menualljson);
        $(document).ready(function () {

            loadTreedata(); //加载树数据

            //关闭
            $("#btnclose").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });
            //添加
            $("#btnsave").click(function(evt){ evt.preventDefault(); save($(this));});
        });

        //加载树的数据
        function loadTreedata()
        {
            $("#treeview").tree({
                data:menualljson,
                checkbox:true,
                onClick:function(node)
                {
                    //if (node.id =="") {
                    //    table_name = $('#treeviw').tree('getParent', node.target).attributes.nodecode;
                    //    types="";
                    //}e
                    //else
                    //{
                    //    table_name=node.attributes.nodecode;
                    //    if (node.attributes.nodelevel !=1) {
                    //        types=node.id;
                    //    }
                    //    else
                    //    {
                    //        types="";
                    //    }
                    //}
                }
            });
        }

        function save(obj)
        {
            var jsondata = {
                roleid:$("#Roleid").val(),
                menuids:[]
            };
            var checkmenus=$("#treeview").tree('getCheckedExt');
            if(checkmenus.length==0){
                alert("没有设置权限");
                return;
            }

            for (var i = 0; i < checkmenus.length; i++) {
                jsondata.menuids.push(checkmenus[i].id);
            }

            //禁用保存按钮，防止重复提交
            $(obj).attr("disabled", true);

            $.ajax({
                type: "post",
                url: baseurl + "Manage/Role/RolemenuSave",
                data: {jsondata: JSON.stringify(jsondata) },
                success: function (e) {
                    $(obj).attr("disabled", false);
                    if (e.Ret == "0") {
                        parent.closeDialog(true);
                    } else {
                        alert(e.Msg);
                    }
                }
            });
        }

    </script>
}

